/*import dbconfig from './demo01/config/dbconfig'
console.log("ccccc")
console.log(dbconfig.mysql_data.map(x => {console.log(x)}))
console.log(dbconfig.mysql_data[0].prototype)
console.log(dbconfig)


for (let key in dbconfig){
    console.log(key)
    console.log(dbconfig[key])

}*/

/*
1.启动数据库
2.获取接口用例
3.执行接口
    1.从公共参数中获取依赖参数
        1.无参数，执行依赖接口
    1.参数化
    2.进行接口配置
    3.执行接口
    4判断返回
    5.执行断言
    6.生成结果
    7.收集结果
    8.存储参数
4.收集存储结果，打印
5.关闭数据库，执行完毕
*/
import db from './demo01/util/mysqlconnect'

import emnu from './demo01/util/emnu'
import httprequest from './demo01/core/samples'
import variables from './demo01/util/variables'
import mongo from 'mongodb'
import _ from 'lodash'
/*db.poolsInit()//初始化数据库池
console.log(variables)
db.query(variables.get(emnu.mysqlpool.user),'select * from `user`.uc_user where app_code = 0 and cell=? ','18512527459')
    .then(data => {
        console.log(data[0])
    })*/
import fly from 'flyio'

//httprequest("http://www.baidu.com",null,null).then(data => console.log(data)).catch(err => console.log(err))


async function cc() {
    await db.poolsInit();
    let url = 'http://10.100.2.36:8082/api/authenticate/mobile_number'
    let reqparams = {
        mobilePhone: '18512527459',
        password: 'aa123456'
    }
    let header = {
        headers: {
            'content-type': "application/json; charset=utf-8",
        },
        method: 'post',
        timeout: 10000,
    }
    await httprequest(url, reqparams, header)
        .then(async data => {
        //console.log(data.data, '====', data.status, '=====', data.statusText)
        let repdata = data.data
        let sql = 'select * from yxx_user where mobile_phone = ?'
      await  db.query(variables.get(emnu.mysqlpool.yingxinxin), sql, [reqparams.mobilePhone])
            .then(vars => {
                //console.log(vars.size())
                console.log(vars.length)
            })

        variables.set('admintoken', data.data.id_token)
        console.log("admintoken存储成功")

    })
        .catch(err => console.log(err))

        await httprequest('http://10.100.2.36:8082/api/account', null, {
        method: 'get',
        headers: {
            Authorization: 'Bearer ' + variables.get('admintoken'),
        },
        Origin: 'http://10.100.2.36',
        'content-type': 'text/plain; charset=utf-8'
    })
        .then(data => console.log( data.data))
        .catch(err => console.log(err))



   db.poolsEnd()
}
function mongoconnect(selectCondition) {
    let  url = 'mongodb://analytics-dev:Wo4heike@10.100.2.12:27017/analytics-dev'
    mongo.MongoClient.connect(url,{ useNewUrlParser: true },function (err,db) {
        if (err)throw err;
        let collection = db.db('analytics-dev')

        // collection.collectionResultCallback(error , collection).error();
        //console.log(collection)

        collection.collection('user_event_data').find(selectCondition).toArray(function (err,result) {
            if (err){
                console.log(`error:${err}`);
            }
            else {
               // console.log(result)
                result.forEach((item,index)=>{
                    console.log(item,item.deviceSet.length)
                })
            }
            db.close()
        })

    })
}

function mongoUpdate(search,update){
    let  url = 'mongodb://analytics-dev:Wo4heike@10.100.2.12:27017/analytics-dev'
    mongo.MongoClient.connect(url,{ useNewUrlParser: true },function (err,db) {
        if (err)throw err;
        let collection = db.db('analytics-dev')

        // collection.collectionResultCallback(error , collection).error();
        //console.log(collection)

        collection.collection('user_event_data').updateOne(search,update,function (err,res) {
            if (err)throw err;
            console.log(res)
            db.close()
        })

    })
}
let condition = {
    productNo: 'LP222790666836520960',//备胎信用 LP221430540032942081，测试产品：LP229699820473679872,汇通天下：LP222790666836520960
    deviceId : '7ea875ed86abd597e04500041',
    eventId : 'clk_third',
    useId    : '',
    channel  :'',
}
let select = {
    //channel : "pc269538250003066880",
    productNo: condition.productNo,
  //  date : '2019-01-08'

}
let update = {
    $set:{
        date : '2019-01-07'
    }
}
let resquestAPP = {
    url : "http://10.200.1.37:8088/api/public/behaviour/collect",
    params : {
        "pageId": "yq_index_page",
        "eventId": condition.eventId,
        "slot": 0,
        "productNo": condition.productNo,
        "productType": "1",
        "type": 1,
        "timestamp": 1540290253539,
        "categoryId": "401",
        "osVersion": "10.3.3",
        "clientIp": "10.20.3.238",
        "mobileType": "iPhone 6",
        "clientCity": "",
        "deviceId": condition.deviceId,
        "clientProvince": "",
        "appSourceId": "1",
        "deviceName": "iPhone",
        "wifiMac": "b4:fb:f9:b7:b8:e0",
        "carrierOperator": "中国联通",
        "appVersion": "1.2.6",
        "lng": "",
        "clientRegion": "",
        "pushId": "cOD/W65Nzt8E31gE15Ezj7dxKUumiYpFAIWxCvBMIdoY+W3BCnP/M0PyMSusa97M",
        "screenHeight": "667",
        "resolution": "750*1334",
        "mac": "02:00:00:00:00:00",
        "appName": "YYYQ",
        "token": "e9f740839c23fb8f008956ed7d53c06f7ab8e5be",
        "networkStatus": "Wifi",
        "screenWidth": "375",
        "lat": "",
        "tId": "",
        "device": "ios",
        "idfa": "1E5D5967-947F-4011-83E9-B9C956C624BC",
        "userId": condition.useId
    },
    header :{
        headers: {
            'content-type': "application/json; charset=utf-8",
        },
        method: 'post',
        timeout: 10000,
    },
}
let resquestChannel = {
    url : "",
    params : {
        brn:"",
        brv:"",
        categoryCode:111,
        clientIp:"",
        dt:"",
        eventId:1212,
        h:"",
        jsv:"",
        net:"",
        osn:"",
        osv:"",
        pageId:111,
        productNo:111,
        promoCode:1111,
        slot:0,
        ts:"",
        type:1,
        w:"",
    },
    header :{
        headers: {
            'content-type': "application/json; charset=utf-8",
        },
        method: 'post',
        timeout: 10000,
    },
}

//mongoUpdate(select,update)
httprequest(resquestAPP.url,resquestAPP.params,resquestAPP.header)
    .then(async data =>{
        console.log(data.status)
        await setTimeout(cc => mongoconnect(select),2000)
        //await
    })
    .catch(err =>console.log(err))
// LP247496769385267200      LP221430540032942080
/*

for(let i=0;i<10;i++){



        let resquestAPP1 = {}
        for(let j in resquestAPP.params){
            resquestAPP1[j] = resquestAPP.params[j]
        }
        resquestAPP1.deviceId= condition.deviceId.slice(0,condition.deviceId.length-1) + i.toString();
        // console.log(condition.deviceId)
        // console.log(resquestAPP1);
        httprequest(resquestAPP.url,resquestAPP1,resquestAPP.header)
            .then(async data =>{
                console.log(resquestAPP1.deviceId)
                //await setTimeout(cc => mongoconnect(select),2000)
                // await mongoconnect(select)
            })
            .catch(err =>console.log(err))

}*/
/*var ob={
    test:1
}*/
/*let aa = {}
for (let i in ob){
   console.log(ob[i])
    aa[i] = ob[i]
}

console.log(aa)*/
/*for (let i=0;i<10;i++){

    let aa = {}
    for (let j in ob) {
        aa[j]=ob[j];
    }

        console.log(aa.test=i)
        setTimeout(()=>console.log(aa.test),2000)

}*/







